package threadpoolnio;

import javax.swing.text.JTextComponent;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author ：xx
 * @date ：Created in 2020/2/27 19:34
 * @description：
 * @modified By：
 */
public class TCPClient2 {
    public static void main(String[] args){
//        client("127.0.0.1",9999);
    }

    public static void client(String ip, int port, JTextComponent txtSend){
        Socket s=null;
        try {
            s = new Socket(ip,port);
            Scanner in=new Scanner(System.in);
            String msg=null;
            PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(),"GBK"));
            while (true) {
                if((msg=txtSend.getText())!=null) {
                    out.println(msg);
                    out.flush();
                    if (msg.equals("q"))
                        break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(s!=null)
                    s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
